/*
 * Sesame Inc.
 * Copyright (c) 2009 All Rights Reserved.
 */
package com.sesame.serviceimpl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.security.providers.encoding.Md5PasswordEncoder;
import org.springframework.ui.Model;

import com.sesame.dao.AlbumDAO;
import com.sesame.dao.AlbumTagDAO;
import com.sesame.dao.CommentDAO;
import com.sesame.dao.ImageDAO;
import com.sesame.dao.ImageTagDAO;
import com.sesame.domain.Admin;
import com.sesame.domain.Album;
import com.sesame.domain.AlbumTag;
import com.sesame.domain.Comment;
import com.sesame.domain.Image;
import com.sesame.domain.ImageTag;
import com.sesame.domain.TagCloud;
import com.sesame.service.AlbumService;
import com.sesame.service.SearchService;
import com.sesame.toolkit.Page;

/**
 * @author voidman
 * 
 */
public class AlbumServiceImpl implements AlbumService {

    private AlbumDAO albumDAO;
    private AlbumTagDAO albumTagDAO;
    private AlbumTag albumTag;
    private ImageTag imageTag;
    private ImageDAO imageDAO;
    private CommentDAO commentDAO;
    private Comment comment;
    private ImageTagDAO imageTagDAO;
    private SearchService searchService;

    public void setAlbumDAO(AlbumDAO albumDAO) {
        this.albumDAO = albumDAO;
    }

    public void setAlbumTagDAO(AlbumTagDAO albumTagDAO) {
        this.albumTagDAO = albumTagDAO;
    }

    public void setAlbumTag(AlbumTag albumTag) {
        this.albumTag = albumTag;
    }

    public void setImageTag(ImageTag imageTag) {
        this.imageTag = imageTag;
    }

    public void setImageDAO(ImageDAO imageDAO) {
        this.imageDAO = imageDAO;
    }

    public void setCommentDAO(CommentDAO commentDAO) {
        this.commentDAO = commentDAO;
    }

    public void setComment(Comment comment) {
        this.comment = comment;
    }

    public void setImageTagDAO(ImageTagDAO imageTagDAO) {
        this.imageTagDAO = imageTagDAO;
    }

    public void setSearchService(SearchService searchService) {
        this.searchService = searchService;
    }

    public boolean isNewAlbum(Album album, Map model) {

        if (albumDAO.getAlbumByTitleAndUserId(album) == null) {
            albumDAO.addAlbum(album);
            searchService.index(album);
            return true;
        } else {
            model.put("error_title", "该标题已经存在！");
            return false;
        }
    }

    public List<Album> listAllAlbum(Page page) {
        List<Album> albumList = albumDAO.getAlbumByUserId(page);
        return albumList;
    }

    public List<Album> listAllAlbum(Integer userId) {
        List<Album> albumList = albumDAO.getAlbumByUserId(userId);
        return albumList;
    }

    public List<Album> listAllAlbum() {
        List<Album> albumList = albumDAO.getAllAlbum();
        return albumList;
    }

    public boolean isEditAlbum(Album album, Map model) {

        if (albumDAO.getAlbumById(album.getId()).getTitle().equals(
                album.getTitle())) {
            albumDAO.updateAlbum(album);
            List<Image> imageList = imageDAO.getImageByAlbumId(album.getId());
            for (Image image : imageList) {
                image.setPrivacy(album.getPrivacy());
                imageDAO.updateImagePrivacy(image);
                searchService.reindex(image);
            }
            searchService.reindex(album);
            return true;
        } else if (albumDAO.getAlbumByTitleAndUserId(album) == null) {
            albumDAO.updateAlbum(album);
            List<Image> imageList = imageDAO.getImageByAlbumId(album.getId());
            for (Image image : imageList) {
                image.setPrivacy(album.getPrivacy());
                imageDAO.updateImagePrivacy(image);
                searchService.reindex(image);
            }
            searchService.reindex(album);
            return true;
        } else {
            model.put("error_title", "该标题已经存在！");
            return false;
        }

    }

    public Album showAlbum(Integer albumId) {
        albumDAO.updateVisitAmount(albumId);
        return (Album) albumDAO.getAlbumById(albumId);

    }

    public void deleteAlbum(Integer albumId) {
        albumTagDAO.deleteAlbumTagByAlbumId(albumId);
        Album album = albumDAO.getAlbumById(albumId);

        List<Image> imageList = imageDAO.getImageByAlbumId(album.getId());
        for (Image image : imageList) {

            searchService.unindex(image);
            imageDAO.deleteImageById(image.getId());
        }
        searchService.unindex(album);
        albumDAO.deleteAlbumById(albumId);
    }

    public void newAlbumTag(Integer albumId, String tag, Map model) {
        if (tag.length() > 20) {
            tag = tag.substring(0, 20);
        }
        albumTag.setAlbumId(albumId);
        String[] tags = tag.split("\\ ");
        for (int i = 0; i < tags.length; i++) {
            albumTag.setTag(tag);
            if (albumTagDAO.getAlbumTagByTagAndUserId(albumTag) == null) {
                albumTag.setAlbumId(albumId);
                albumTag.setTag(tag);
                albumTagDAO.addAlbumTag(albumTag);
    
            } else {
                model.put("error_tag", "该标签已经存在！");
            }
        }
    }

    public void deleteAlbumTag(Integer albumTagId) {
        albumTagDAO.deleteAlbumTagById(albumTagId);
    }

    public List<Image> listImage(Page page) {

        return imageDAO.getImageByAlbumId(page);
    }

    public List<Image> listImage(Integer albumId) {

        return imageDAO.getImageByAlbumId(albumId);
    }

    public List<Image> listAllImage() {

        return imageDAO.getAllImage();
    }

    public Image showImage(Integer imageId) {
        imageDAO.updateVisitAmount(imageId);
        return imageDAO.getImageById(imageId);
    }

    public boolean isEditImage(Image image, Map model) {
        Album album = albumDAO.getAlbumById(image.getAlbumId());
        if (album == null) {
            model.put("error_album", "目标相册不存在！");
            return false;
        }
        if (!album.getUserId().equals(image.getUserId())) {
            model.put("error_album", "没有权限移动到该相册！");
            return false;
        }

        imageDAO.updateImage(image);
        image = imageDAO.getImageById(image.getId());
        searchService.reindex(image);

        return true;
    }

    public List<Comment> listComment(Integer imageId) {
        return commentDAO.getCommentByImageId(imageId);
    }

    public void newComment(Integer imageId, Integer userId, String nickName,
            String content, String headPic) {
        comment.setImageId(imageId);
        comment.setUserId(userId);
        comment.setNickName(nickName);
        comment.setContent(content);
        comment.setHeadPic(headPic);
        Date createTime = new Date();
        comment.setCreateTime(createTime);
        commentDAO.addComment(comment);

    }

    public void deleteComment(Integer id) {
        commentDAO.deleteCommentById(id);
    }

    public void newImage(Image image, Map model) {
        imageDAO.addImage(image);
        searchService.index(image);
    }

    public List<ImageTag> listImageTag(Integer imageId) {
        return (List<ImageTag>) imageTagDAO.getAllImageTagByImageId(imageId);
    }

    public void newImageTag(Integer ImageId, String tag, Map model) {
        if (tag.length() > 20) {
            tag = tag.substring(0, 20);
        }
        imageTag.setImageId(ImageId);
        String[] tags = tag.split("\\ ");
        for (int i = 0; i < tags.length; i++) {
            imageTag.setTag(tag);
            if (imageTagDAO.getImageTagByTagAndImageId(imageTag) == null) {
                imageTag.setImageId(ImageId);
                imageTag.setTag(tag);
                imageTagDAO.addImageTag(imageTag);
    
            } else {
                model.put("error_tag", "该标签已经存在！");
            }
        }
    }

    public void deleteImageTag(Integer imageTagId) {
        imageTagDAO.deleteImageTagById(imageTagId);
    }

    public void updateCover(Album album) {
        searchService.reindex(album);
        albumDAO.updateCover(album);
    }

    public Integer getAlbumCount(Integer userId) {
        return albumDAO.getAlbumCount(userId);
    }

    public Integer getAllAlbumCount() {
        return albumDAO.getAllAlbumCount();
    }

    public List<Album> listTopAlbum() {
        List<Album> albumList = albumDAO.getTopAlbum();
        return albumList;
    }

    public List<Album> listTopAlbumByUserId(Integer userId) {
        List<Album> albumList = albumDAO.getTopAlbumByUserId(userId);
        return albumList;
    }

    public List<Image> listTopImage(Integer userId) {

        return imageDAO.getTopImageByUserId(userId);
    }

    public List<Image> listAllImage(Integer userId) {

        return imageDAO.getImageByUserId(userId);
    }

    public List<Image> listTopImage() {

        return imageDAO.getTopImage();
    }

    public void deleteImage(Integer id) {
        imageTagDAO.deleteImageTagByImageId(id);
        Image image = imageDAO.getImageById(id);
        searchService.unindex(image);
        imageDAO.deleteImageById(id);
    }

    public List<TagCloud> listAllAlbumTag() {

        return albumTagDAO.getAllAlbumTag();
    }

    public List<TagCloud> listAllImageTag() {

        return imageTagDAO.getAllImageTag();
    }

    public List<Album> searchAlbumTag(String tag) {
        List<Album> albumList = new ArrayList<Album>();
        List<AlbumTag> albumTagList = (List<AlbumTag>) albumTagDAO
                .getAlbumTagByTag(tag);
        for (int i = 0; i < albumTagList.size(); i++) {
            albumList.add(albumDAO.getAlbumById(albumTagList.get(i)
                    .getAlbumId()));
        }
        return albumList;

    }

    public List<AlbumTag> listAlbumTag(Integer albumId) {

        return albumTagDAO.getAlbumTagByAlbumId(albumId);
    }

    public List<Album> listPageAlbum(Page page) {
        List<Album> albumList = albumDAO.getAllAlbum(page);
        return albumList;
    }

    public Integer getImageCount(Integer albumId) {
        return imageDAO.getImageCount(albumId);
    }

    public List<Image> listPageImage(Page page) {
        List<Image> imageList = imageDAO.getAllImage(page);
        return imageList;
    }

    public List<Image> listUserImage(Page page) {
        List<Image> imageList = imageDAO.getImageByUserId(page);
        return imageList;
    }

    public Integer getAllImageCount() {

        return imageDAO.getAllImageCount();
    }

    public Integer getUserImageCount(Integer userId) {
        return imageDAO.getUserImageCount(userId);

    }
}